//
//  SinaHomeCustomRefreshView.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/23.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaHomeCustomRefreshView.h"

@interface SinaHomeCustomRefreshView ()

@property (nonatomic,weak) UIImageView *arrowIV;
@property (nonatomic,weak) UILabel *descLbl;
@property (nonatomic,weak) UITableView *fatherView;
//菊花
@property (nonatomic,weak) UIActivityIndicatorView *refreshCycle;
@property(nonatomic,assign) SinaRefresh newState;
@property (nonatomic,assign) SinaRefresh oldState;

@end

@implementation SinaHomeCustomRefreshView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


#pragma mark
#pragma mark - 结束刷新
-(void)endRefreshing{
    
    self.newState = SinaRefreshStateNormal;
    
}



#pragma mark
#pragma mark - 布局界面
-(void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self setupArrowIV];
    
    [self setupDescLbl];
    
    [self setupRefreshIndecator];
    
    //初始状态为normal
    self.newState = SinaRefreshStateNormal;
    self.oldState = SinaRefreshStateNormal;
}

#pragma mark
#pragma mark - 设置菊花
-(void)setupRefreshIndecator{
    UIActivityIndicatorView* refreshCycle = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _refreshCycle = refreshCycle;
    [self addSubview:refreshCycle];
    
    [refreshCycle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.arrowIV);
    }];
}

#pragma mark
#pragma mark - 设置指示箭头
-(void)setupArrowIV{
    UIImageView *arrowIV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableview_pull_refresh"]];
    _arrowIV = arrowIV;
    [self addSubview:arrowIV];
    
    [arrowIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).mas_offset(-50);
    }];
    
}


#pragma mark
#pragma mark - 设置提示lable
-(void)setupDescLbl{
    UILabel *descLable = [[UILabel alloc]init];
    _descLbl = descLable;
    descLable.text = @"下拉起飞哦~";
    descLable.textColor = [UIColor darkGrayColor];
    descLable.font = [UIFont systemFontOfSize:18];
    
    [self addSubview:descLable];
    [descLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.equalTo(self.arrowIV.mas_trailing).offset(8);
    }];
    
}


#pragma mark
#pragma mark - 添加到父视图上会触发方法
-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    UITableView *fatherView = (UITableView *)newSuperview;
    _fatherView = fatherView;
    if([fatherView isKindOfClass:[UITableView class]]){
        [fatherView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
    
}


#pragma mark
#pragma mark - 改变状态时触发的方法
-(void)setNewState:(SinaRefresh)newState{
    
    _newState = newState;
    
    switch (newState) {
        case SinaRefreshStateNormal:
        {
            self.descLbl.text = @"下拉起飞哦~~";
            [self.refreshCycle stopAnimating];
            self.arrowIV.hidden = NO;
            
            if(self.oldState == SinaRefreshStateReady){
                [UIView animateWithDuration:0.25 animations:^{
                    self.arrowIV.transform = CGAffineTransformIdentity;
                }];
                self.oldState = SinaRefreshStateNormal;
            }else if (self.oldState == SinaRefreshStateRefreshing){
                self.arrowIV.transform = CGAffineTransformIdentity;
                [UIView animateWithDuration:0.5 animations:^{
                    
                    self.fatherView.contentInset = UIEdgeInsetsMake(kGuideBarHeight, 0, 0, 0);
                }];
                self.oldState = SinaRefreshStateNormal;
            }
        }
            break;
        case SinaRefreshStateReady:
        {
            self.descLbl.text = @"放手刷新....";
            [UIView animateWithDuration:0.25 animations:^{
                self.arrowIV.transform = CGAffineTransformMakeRotation(M_PI);
            }];
            self.oldState = SinaRefreshStateReady;
        }
            break;
        case SinaRefreshStateRefreshing:
        {
            self.descLbl.text = @"正在刷新....";
            self.arrowIV.hidden = YES;
            [self.refreshCycle startAnimating];
            self.fatherView.contentInset = UIEdgeInsetsMake(kCustomRefreshHeight + kGuideBarHeight, 0, 0, 0);
            self.oldState = SinaRefreshStateRefreshing;
        }
            break;
            
        default:
            break;
    }
    
}







#pragma mark
#pragma mark - 触发KVO方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    CGFloat offsetY = ((NSValue *)change[@"new"]).CGPointValue.y;
//    NSLog(@"%f",offsetY);
    if(self.fatherView.isDragging){
        
        if(self.oldState == SinaRefreshStateNormal && offsetY < -(kGuideBarHeight + kCustomRefreshHeight)){
            self.newState = SinaRefreshStateReady;
        }else if ((self.oldState == SinaRefreshStateReady) && offsetY > -(kGuideBarHeight + kCustomRefreshHeight)){
            self.newState = SinaRefreshStateNormal;
        }
        
    }else{
        
        if((![self.refreshCycle isAnimating]) && self.oldState == SinaRefreshStateReady){
            self.newState = SinaRefreshStateRefreshing;
            
            //发送事件
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
        
    }
    
}









#pragma mark
#pragma mark - 移除监听
-(void)dealloc{
    [_fatherView removeObserver:self forKeyPath:@"contentOffset"];
}


@end
